/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gac.gacmod.visual;

import java.awt.MouseInfo;
import java.awt.Point;
import javax.swing.SwingUtilities;
import org.netbeans.api.visual.action.MoveProvider;
import org.netbeans.api.visual.widget.Widget;

/**
 *
 * @author Tomek
 */
class MyMoveProvider implements MoveProvider
{

    private BoxScene _scene;
    private DropAreaWidget _lastDropAreaWidget;
    //private Widget _dragWidget;

    public MyMoveProvider(BoxScene scene)
    {
        _scene = scene;
//        _dragWidget = new LabelWidget( scene, "DUPA");
//        _dragWidget.setOpaque(true);
//        _dragWidget.setBackground(Color.RED);
//        _dragWidget.setVisible(false);
//        _scene.addChild(_dragWidget);
    }

    public void movementStarted(Widget arg0)
    {
        //_scene.setDropAreasVisible(true);
        if( arg0 instanceof StepWidgetBase )
            _scene.stepWidgetSelected((StepWidgetBase) arg0);
        //_dragWidget.setVisible(true);
        //_dragWidget.setPreferredLocation(arg0.convertLocalToScene(arg0.getLocation()));
        //System.out.println("start");
    }

    public void movementFinished(Widget arg0)
    {
        //_scene.setDropAreasVisible(false);
        //System.out.println("stop");
        //_dragWidget.setVisible(false);
        if (_lastDropAreaWidget != null)
        {
            _lastDropAreaWidget.Drop((StepWidgetBase) arg0);
        }
        
        _scene.turnHighlightOff();

    }

    public Point getOriginalLocation(Widget arg0)
    {
        //return arg0.convertLocalToScene(arg0.getLocation());
//        Widget w = arg0;
//        while( w != null && !(w instanceof StepWidgetBase ))
//        {
//            w = w.getParentWidget();
//        }
//        
//        if( w != null )
//        {
//            Point parentAbs = w.convertLocalToScene(w.getLocation());
//            Point childAbs = arg0.convertLocalToScene(arg0.getLocation());
//            return new Point( parentAbs.x - childAbs.x, parentAbs.y - childAbs.y );
//        }
//        
//        throw new UnsupportedOperationException( "Wrong D&D source!");
        
        
        Point mouseCoord = MouseInfo.getPointerInfo().getLocation();
        //System.out.println("Scr: " + mouseCoord);
        SwingUtilities.convertPointFromScreen(mouseCoord, _scene.getView());
        //System.out.println("JCo: " + mouseCoord);
        Point sceneCoord = _scene.convertViewToScene(mouseCoord);
        //System.out.println("Scn: " + sceneCoord);
        Point widgetCoord = arg0.convertSceneToLocal(sceneCoord);
        //System.out.println("Wgt: " + widgetCoord);
        
        return widgetCoord;
        //return arg0.getLocation();
    }

    public void setNewLocation(Widget arg0, Point arg1)
    {
//        _lastDropAreaWidget = _scene.FindBestDropArea(arg0.convertLocalToScene(
//                arg1));
        _lastDropAreaWidget = _scene.FindBestDropArea(arg0, arg1);

        //if( _lastDropAreaWidget != null )
        //    _scene.getView().setCursor( Cursor.);
        //_dragWidget.setPreferredLocation(arg1);
        //_scene.revalidate();
        //System.out.println( "New location set to: " + _lastDropAreaWidget);

    }
}
